define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/index' + location.search,
                    add_url: 'goods/add',
                    edit_url: 'goods/edit',
                    del_url: 'goods/del',
                    multi_url: 'goods/multi',
                    import_url: 'goods/import',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'goods_no', title: __('Goods_no'), operate: 'LIKE'},
                        {field: 'category.name', title: __('Category_id'),searchable:false},
                        {
                            field: 'category_id',
                            title: __('Category_id'),
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="goods_category/index" data-field="name"',
                            operate: 'in',
                            formatter: Table.api.formatter.search
                        },

                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'person_max', title: __('Person_max'), operate: 'RANGE'},
                        {field: 'person_real', title: __('Person_real'), operate: 'RANGE'},

                        {field: 'departtime', title: __('Departtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,datetimeFormat: 'YYYY-MM-DD'},
                        {field: 'uptotime', title: __('Uptotime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,datetimeFormat: 'YYYY-MM-DD'},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'office_id', title: __('Office_id')},
                        // {field: 'shop_id', title: __('Shop_id')},

                        // {field: 'manystore.username', title: __('Store_id'),searchable:false},
                        // {
                        //     field: 'Store_id',
                        //     title: __('Store_id'),
                        //     visible: false,
                        //     addclass: 'selectpage',
                        //     extend: 'data-source="auth.manystore/index" data-field="username"',
                        //     operate: 'in',
                        //     formatter: Table.api.formatter.search
                        // },

                        {field: 'branchofficeoffice.name', title: __('Office_id'), operate: 'LIKE'},
                        {field: 'manystoreshop.name', title: __('Shop_id'), operate: 'LIKE'},
                        {field: 'manystore.username', title: __('Store_id'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, table: table, formatter: Table.api.formatter.status},
                        {field: 'switch', title: __('Switch'), searchList: {"1":__('Switch 1'),"0":__('Switch 0')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: '报名记录',
                                    text: __('报名记录'),
                                    classname: 'btn btn-info btn-dialog btn-small',
                                    icon: 'fa',
                                    url: 'goods_customer/index?goods_id={ids}',


                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.initvalidate();
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.initvalidate();
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            initvalidate:function (){
                $.validator.config({
                    rules: {
                        required: function (element) {
                            if (!element.value.toString()) {
                                // console.log(element);
                                // console.log(element.value);
                                $(element).parents('.tab-pane').addClass('active').addClass('in');
                                $(element).parents('.tab-pane').siblings().removeClass('active').removeClass('in');
                                let index = $(element).parents('.tab-pane').index();
                                $(".nav-tabs>li").removeClass('active').eq(index).addClass('active');
                                return '请输入或者选择';
                            }

                        }
                    }
                });
            }
        }
    };
    return Controller;
});